<?php

/**
 * 用户帐号控制器
 *
 */

namespace app\controllers\user;

use app\common\base\ApiController;
use app\common\behaviors\ApiCommonBehavior;
use Codeception\Step\Condition;

class AccountController extends ApiController
{

    /**
     * 登陆;
     */
    public function actionLogin()
    {
        $this->rules = [
            [['username', 'pwd'], 'required'],
            [['username', 'pwd'], 'string'],
            [['type'], 'integer'],
            ['type', 'default', 'value' => 1],
        ];
        $inputs = $this->validate();

        $info = ApiCommonBehavior::call(
            'user',
            'AccountService',
            'login',
            $inputs['username'],
            $inputs['pwd'],
            $inputs['type']
        );
        return $info;
    }

    /**
     * 注册;
     */
    public function actionRegister()
    {
        $this->rules = [
            [['nickname', 'pwd', 'username'], 'required'],
            [['nickname', 'pwd', 'username'], 'string'],
            ['email', 'email'],
            [['type'], 'integer'],
            ['email', 'default', 'value' => ''],
            ['type', 'default', 'value' => 1],
        ];
        $inputs = $this->validate();
        
        $result = ApiCommonBehavior::call(
            'user',
            'AccountService',
            'register',
            $inputs['username'],
            $inputs['nickname'],
            $inputs['pwd'],
            $inputs['type'],
            $inputs['email']
        );
        
        return $result;
    }
    
    /**
     * 个人信息;
     */
    public function actionUserInfo(){
        $userId = $this->userInfo->id;
        $info = ApiCommonBehavior::call('user', 'AccountService', 'info', [
            'condition' => [
                'id' => $userId,
            ]
        ]);
        unset($info['pwd']);

        return [
            'info' => $info,
        ];
    }

}
